API Corner

A Numbers-Only Textbox

Home

View 3D

Math for LB

Using the Modem

Functions

Subroutines

Hall of Fame

API Corner

Tip Corner - RUN

Clipart Viewer

User Design Graphics

Sort by Surnames

Many Textboxes

Newsletter help

Index


API Zone... Beware!

It is best to understand the use of API calls in Liberty BASIC before using this technique. Earlier issues of the newsletter include tutorials for calling the Windows API from Liberty BASIC, and for creating an manipulating controls with API calls. This method is meant for the more experienced Liberty BASIC programmer.

Changing the Style of a Liberty BASIC Control

We can easily modify a Liberty BASIC textbox control so that it accepts only numeric input when the user types. Any non-number characters do not appear and a warning beep sounds when they are typed. To do this, we change the STYLE of the textbox. For a complete description of this method, see issue #111. Here is a short explanation:

Changing the style of a control requires three steps.

  1. Get the style bits of the control into a variable with GetWindowLongA and a flag of _GWL_STYLE
  2. Modify the style by putting the bits together with bitwise operators OR, XOR, depending on whether a style bit is to be added or removed from the original style bits.
  3. Give the control the modified style with SetWindowLongA

A Textbox That Accepts Only Numeric Input

To change the Liberty BASIC textbox so that it accepts only numeric input requires that we ADD a style to the existing control style. To do this, it is put together with the existing style with the OR operator. To add the style bit:

    newStyle = origStyle OR _ES_NUMBER

DEMO

Change the style of a Liberty BASIC textbox so that it accepts only numeric input.

'** Created by LB Workshop - 9/19/2003 6:46:08 AM
'** Numbers-Only TextBox

    True = 1 : False = 0

[WindowSetup]
    NOMAINWIN
    WindowWidth = 220 : WindowHeight = 140
    UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
    UpperLeftY = INT((DisplayHeight-WindowHeight)/2)

[ControlSetup]
Menu        #main, "&File" , "E&xit", [quit]
textbox     #main.t, 15, 40, 170, 25

Open "Numbers-Only TextBox" for Window as #main

    #main "trapclose [quit]"
    #main "font ms_sans_serif 10"
    style=GetWindowLong(hwnd(#main.t),_GWL_STYLE)
    style=style or _ES_NUMBER
    ret=SetWindowLong(hwnd(#main.t),_GWL_STYLE,style)
    
    #main.t "!setfocus"

[loop]
    Wait

[quit]
    close #main : END

Function GetWindowLong(hW, type)
    CallDLL #user32, "GetWindowLongA",_
    hW As Long, type As Long,GetWindowLong As Long
    End Function

Function SetWindowLong(hW,index,newVal)
    CallDLL #user32, "SetWindowLongA",_
    hW As Long,index As Long,_
    newVal As Long,SetWindowLong As Long
    End Function


Home

View 3D

Math for LB

Using the Modem

Functions

Subroutines

Hall of Fame

API Corner

Tip Corner - RUN

Clipart Viewer

User Design Graphics

Sort by Surnames

Many Textboxes

Newsletter help

Index